﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<SG.Domain.Periodo>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Periodos
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="Head" runat="server">
  <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid.css") %>" rel="stylesheet" type="text/css" />   
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid_skins.css") %>" rel="stylesheet" type="text/css" />
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/skins/dhtmlxgrid_dhx_blue.css") %>" rel="stylesheet" type="text/css" />


    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxcommon.js") %>" type="text/javascript"></script>
     <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgridcell.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/ext/dhtmlxgrid_filter.js") %>" type="text/javascript"></script>
    
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


<%
      
       string array = "";
       foreach (var item in Model)
       {
           array += ",['"+
                        item.Id + "','" +   
                        item.NumeroPeriodo.ToString("000000") + "','" +
                        String.Format("{0:g}", item.Fecha) + "','" +                    
                        item.Demanda + "','" +
                        item.Promedio + "','" +
                        item.Prediccion + "','" +
                        item.Tendencia + "','" +
                        item.Indice + 
                        
                    "']";
                       
       }
       if (string.IsNullOrEmpty(array))
           array = "Array()";
       else 
           array =  "["+array.Substring(1)+"]";
       
        %>
            
           
            <div id="gridList" style="height:100%"></div>
    <p>
        Periodo: <input id="txtPeriodo" type="text" value="0" />
        <a href="javascript:predecir()">Predecir</a>&nbsp;&nbsp;<a href="javascript:verDetallePeriodos()">Detalle</a>
    </p>
    
    <script type="text/javascript">
     
      var jarray = <%=(array??"new array()") %>;

        var mygrid;
      

          window.onload=doOnLoad;
        function doOnLoad(){
           
            mygrid = new dhtmlXGridObject('gridList');
            mygrid.setImagePath("<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/imgs/") %>");
            mygrid.setHeader(",Número de periodo, Fecha,Demanda, Promedio,Predicción,Tendencia, Indice",null,["text-align:center","text-align:center","text-align:center","text-align:center","text-align:center","text-align:center","text-align:center"]);
            mygrid.attachHeader(",#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter");
            mygrid.setInitWidths("0,220,120,120,120,120,120,*");
            mygrid.setColAlign("center,left,left,left,left,left,left");
            mygrid.setColTypes("ed,ed,ed,ed,ed,ed,ed");
            mygrid.setColumnIds("ID,NROPERIODO,FECHA,DEMANDA,PROMEDIO,PREDICCION,TENDENCIA,INDICE");
            
            mygrid.setSkin("light");
            mygrid.setColSorting("int,str,str,str,str,str,str");
            
            mygrid.enableAutoHeight(true);
           

            
            mygrid.init();
            mygrid.parse(jarray, "jsarray");


          
           
        }

        function predecir(){
            $.ajax({
                url: '<%=Url.Action("PredecirPeriodo")%>',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data:   'idArticulo=<%= TempData["idArticulo"]%>'+
                        '&periodo='+$("#txtPeriodo").val(),
                success: function(data) {
                  
                        if(data.status==0){
                            alert(data.prediccion);
                        }else 
                            alert("Error:" + data.err);
                    
                    },
                    error:function (er1,er2,er3){
                        alert("Error al procesar la solicitud"+er2+"\n"+er1);
                    }
            });
        }

         function verDetallePeriodos(){
            var idRow=mygrid.getSelectedId();
            if(idRow==null || idRow<=0) {
                alert("Debe seleccionar una fila");
                return;
            }else{
                $.ajax({
                url: '<%=Url.Action("DetallePeriodo")%>',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data:   "idPeriodo="+mygrid.cells(idRow,0).getValue(),
                success: function(data) {
                  
                        if(data.status==0){
                            alert("Detalles para el Período Seleccionado:"+"\n"+
                                    "\n"+    
                                    "Valor del Error: "+data.ValorError+"\n"+
                                    "\n"+
                                    "Desviación Estandar: "+data.DesviacionEstandar+"\n"+
                                    "\n"+
                                    "ErrorPromedio: "+data.ErrorPromedio+"\n"+
                                    "\n"+
                                    "MSEn: "+data.MSEn+"\n"+
                                    "\n"+
                                    "Señal de Rastreo: "+data.SenialRastreo+"\n"+
                                    "\n"+
                                    "\n"+
                                    data.mensaje+"\n");
                        }else 
                            alert("Error:" + data.err);
                    
                    },
                    error:function (er1,er2,er3){
                        alert("Error al procesar la solicitud"+er1+"\n"+er1);
                    }
            });
            }
        
        }
        
    </script>
    
   
</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="TitlePage" runat="server">
Periodos
</asp:Content>

<asp:Content ID="Content5" ContentPlaceHolderID="HeaderLink" runat="server">
</asp:Content>

